#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
________________________________________________
 @Time    : 2018/6/6 上午11:15
 @Author  : NickTet
 @Site    : 
 @File    : ProxyValidSchedule.py
 @Software: PyCharm
 _______________________________________________
 """
from Schedule.ProxyCheck import ProxyCheck
from Manager.ProxyManager import ProxyManager
import sys
sys.path.append("../")
import time

try:
    from queue import Queue  # py3
except:
    from queue import Queue  # py2


class ProxyValidSchedule(ProxyManager, object):
    def __init__(self):
        ProxyManager.__init__(self)
        self.queue = Queue()
        self.proxy_item = dict()

    def __validProxy(self, threads=10):
        thread_list = list()
        for index in range(threads):
            thread_list.append(ProxyCheck(self.queue, self.proxy_item))

        for thread in thread_list:
            thread.daemon = True
            thread.start()

        for thread in thread_list:
            thread.join()

    def main(self):
        self.putQueue()
        while True:
            if not self.queue.empty():
                self.log.info("Start valid useful proxy")
                self.__validProxy()
            else:
                self.log.info("Valid Complete! sleep 5 minutes.")
                time.sleep(60 * 5)
                self.putQueue()
        pass

    def putQueue(self):
        self.db.changeTable(self.useful_proxy_queue)
        self.proxy_item = self.db.getAll()
        if self.proxy_item:
            for item in self.proxy_item:
                self.queue.put(item)


def run():
    p = ProxyValidSchedule()
    p.main()


if __name__ == "__main__":
    p = ProxyValidSchedule()
    p.main()
